import { Controller, Get, Post, Body, Param, Query, Patch, UseGuards } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiParam, ApiQuery, ApiBody, ApiBearerAuth } from '@nestjs/swagger';
import { LogService } from './log.service';
import { Admin } from 'src/common/decorators/admin.decorator';

@ApiTags('logs')
@ApiBearerAuth()
@Controller('api/admin/logs')
export class LogController {
  constructor(private readonly logService: LogService) {}

  @Admin()
  @Get('admin-operations')
  @ApiOperation({ summary: '获取管理员操作日志' })
  @ApiQuery({ name: 'page', required: false, description: '页码', example: 1 })
  @ApiQuery({ name: 'pageSize', required: false, description: '每页大小', example: 50 })
  @ApiQuery({ name: 'operationType', required: false, description: '操作类型' })
  @ApiQuery({ name: 'adminId', required: false, description: '管理员ID' })
  @ApiQuery({ name: 'startDate', required: false, description: '开始日期' })
  @ApiQuery({ name: 'endDate', required: false, description: '结束日期' })
  @ApiQuery({ name: 'searchText', required: false, description: '搜索文本' })
  async getAdminOperations(
    @Query('page') page?: number,
    @Query('pageSize') pageSize?: number,
    @Query('operationType') operationType?: string,
    @Query('adminId') adminId?: number,
    @Query('startDate') startDate?: string,
    @Query('endDate') endDate?: string,
    @Query('searchText') searchText?: string,
  ) {
    return await this.logService.getAdminOperations(
      page || 1,
      pageSize || 50,
      operationType,
      adminId,
      startDate ? new Date(startDate) : undefined,
      endDate ? new Date(endDate) : undefined,
      searchText,
    );
  }


  @Admin()
  @Get('login-logs')
  @ApiOperation({ summary: '获取登录日志' })
  @ApiQuery({ name: 'page', required: false, description: '页码', example: 1 })
  @ApiQuery({ name: 'pageSize', required: false, description: '每页大小', example: 50 })
  @ApiQuery({ name: 'loginResult', required: false, description: '登录结果：1=成功, 2=失败, 3=警告' })
  @ApiQuery({ name: 'adminId', required: false, description: '管理员ID' })
  @ApiQuery({ name: 'startDate', required: false, description: '开始日期' })
  @ApiQuery({ name: 'endDate', required: false, description: '结束日期' })
  @ApiQuery({ name: 'searchText', required: false, description: '搜索文本' })
  async getLoginLogs(
    @Query('page') page?: number,
    @Query('pageSize') pageSize?: number,
    @Query('loginResult') loginResult?: number,
    @Query('adminId') adminId?: number,
    @Query('startDate') startDate?: string,
    @Query('endDate') endDate?: string,
    @Query('searchText') searchText?: string,
  ) {
    return await this.logService.getLoginLogs(
      page || 1,
      pageSize || 50,
      loginResult,
      adminId,
      startDate ? new Date(startDate) : undefined,
      endDate ? new Date(endDate) : undefined,
      searchText,
    );
  }

  @Admin()
  @Get('security-events')
  @ApiOperation({ summary: '获取安全事件' })
  @ApiQuery({ name: 'page', required: false, description: '页码', example: 1 })
  @ApiQuery({ name: 'pageSize', required: false, description: '每页大小', example: 50 })
  @ApiQuery({ name: 'securityLevel', required: false, description: '安全级别：0=普通, 1=警告, 2=严重, 3=紧急' })
  @ApiQuery({ name: 'isRead', required: false, description: '是否已读：0=未读, 1=已读' })
  @ApiQuery({ name: 'startDate', required: false, description: '开始日期' })
  @ApiQuery({ name: 'endDate', required: false, description: '结束日期' })
  @ApiQuery({ name: 'searchText', required: false, description: '搜索文本' })
  async getSecurityEvents(
    @Query('page') page?: number,
    @Query('pageSize') pageSize?: number,
    @Query('securityLevel') securityLevel?: number,
    @Query('isRead') isRead?: number,
    @Query('startDate') startDate?: string,
    @Query('endDate') endDate?: string,
    @Query('searchText') searchText?: string,
  ) {
    return await this.logService.getSecurityEvents(
      page || 1,
      pageSize || 50,
      securityLevel,
      isRead,
      startDate ? new Date(startDate) : undefined,
      endDate ? new Date(endDate) : undefined,
      searchText,
    );
  }

  @Admin()  
  @Patch('security-events/:id/read')
  @ApiOperation({ summary: '标记安全事件为已读' })
  @ApiParam({ name: 'id', description: '日志ID' })
  async markSecurityEventAsRead(@Param('id') id: string) {
    await this.logService.markSecurityEventAsRead(parseInt(id));
    return { message: '安全事件已标记为已读' };
  }

  @Admin()  
  @Patch('security-events/:id/resolve')
  @ApiOperation({ summary: '解决安全事件' })
  @ApiParam({ name: 'id', description: '日志ID' })
  @ApiBody({ 
    schema: {
      type: 'object',
      properties: {
        resolution: { type: 'string', description: '处理说明' }
      }
    }
  })
  async resolveSecurityEvent(
    @Param('id') id: string,
    @Body('resolution') resolution: string,
  ) {
    await this.logService.resolveSecurityEvent(parseInt(id), resolution);
    return { message: '安全事件已解决' };
  }

  @Admin()  
  @Get('statistics')
  @ApiOperation({ summary: '获取日志统计信息' })
  async getStatistics() {
    return await this.logService.getStatistics();
  }

  @Admin()  
  @Post('cleanup')
  @ApiOperation({ summary: '清理过期日志（保留最近30天）' })
  async cleanupOldLogs() {
    const deletedCount = await this.logService.cleanupOldLogs();
    return { message: `已清理${deletedCount}条过期日志记录` };
  }
}